/**************************************************************************************
*
*	Copyright (C) 2009 - 2012 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	GraphicsBuffer.cpp
*
*	Comments	-	See GraphicsBuffer.h
*
**************************************************************************************/
#include "../Include/GraphicsBuffer.h"
#include "../Include/GraphicsDevice.h"
#include "../Include/Engine.h"
#include "../Include/Renderer.h"
#include "../Include/DX11Mapping.h"

namespace Pulse
{
	GraphicsBuffer::GraphicsBuffer( void )
		: m_pBuffer( PSX_NULL )
	{
		m_pRenderer = Engine::GetInstance()->GetRenderer();
	}

	GraphicsBuffer::~GraphicsBuffer( void )
	{

	}

	void GraphicsBuffer::Map( MappedResource *pMapRes, UINT subResourceIndex, EResourceMapping::Type mapType )
	{
		D3D11_MAPPED_SUBRESOURCE dxMap;

		ID3D11DeviceContext *pDevice = (ID3D11DeviceContext*)m_pRenderer->GetNakedDeviceContext();

		HRESULT hr = pDevice->Map( m_pBuffer, subResourceIndex, DX11Mapping::ToDXMapping( mapType ), 
			0, &dxMap ); 

		if ( hr == S_OK )
		{
			pMapRes->pData		= (BYTE*)dxMap.pData;
			pMapRes->DepthPitch = dxMap.DepthPitch;
			pMapRes->RowPitch	= dxMap.RowPitch;
		}
		else
		{
			PSX_PushWarning( "Failed to map resource buffer." );
			PSX_ZeroMem( pMapRes, sizeof(MappedResource) );
		}
	}

	void GraphicsBuffer::UnMap( UINT subResourceIndex )
	{
		ID3D11DeviceContext *pDevice = (ID3D11DeviceContext*)m_pRenderer->GetNakedDeviceContext();

		pDevice->Unmap( m_pBuffer, subResourceIndex );
	}

	void GraphicsBuffer::ReleaseBuffer( void )
	{
		PSX_SafeRelease( m_pBuffer );
	}

}